<?php
/**
 * 开放平台基础类文件 授权及消息通知的实现
 */
namespace app\common\helper\upload;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Exception;
class UploadQiniuHandler{
    private $config;
    private $url;
    public function __construct($config) {
        $this->config = $config;
        $this->url = rtrim($config['domain'],'\/');
    }
    
    public function upload($file,$name){
        $auth = new Auth(trim($this->config['qiniu']['ak']), trim($this->config['qiniu']['sk']));
        $token = $auth->uploadToken(trim($this->config['qiniu']['bucket']));
        $filePath = $file['tmp_name'];
        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($token, $name, $filePath);
        if($err !== null){
            throw new Exception('上传七牛云失败');
        }
        return  [
            'url'   => $this->url.'/'.$ret['key'],
            'hash'  => $ret['hash'],
            'key'   => $ret['key'],
        ];
    }
    
    public function delete(){
        
    }
    
}